import numpy as np
import pandas as pd

arr = np.random.randint(1, 20, size=(3, 3))
df = pd.DataFrame(arr, columns=['a', 'b', 'c'])
print('原始数据: \n', df)
print('每列求和聚变: \n', df.agg('sum'))
print('每列同时求和及平均值的聚合: \n', df.agg(['sum', 'mean']))


def rang(arr):
    return arr.max() - arr.min()


print('各行分别求和、平均值和极差聚合 :\n', df.agg({0: 'sum', 1: 'mean', 2: rang}, axis=1))
